<?php

class Categories extends Onecore_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model("categories_model");
        $this->_moduleName = utf8_encode("Modulo modulos_model");
        $this->_moduleDescription = utf8_encode("Description") . $this->_moduleName;
        $this->_modelInstance = $this->categories_model;
    }
    public function loadTypeResourcesList(){
        $order = array();
        $conditions = new stdClass();
        $conditions->start = $this->input->get('start');
        $conditions->limit = $this->input->get('limit');
        $type = 3;
        
        $cond = array(array('type'=>'where','field'=>'type_objects_id','value'=>$type));
        
        $order['field'] = 'name';
        $order['direction'] = 'ASC';
        
        $data = $this->_modelInstance->loadList($order,null,$cond);
        
        $res = array(
            "success" => true,
            "results" => $data['results'],
            "rows" => $data['items']
        );
        $this->layout->printLike(json_encode($res), "json");
    }
    public function loadList(){
        $order = array();
        $conditions = new stdClass();
        $conditions->start = $this->input->get('start');
        $conditions->limit = $this->input->get('limit');
        $type = $this->input->get('type_objects_id');
        
        $cond = array(array('type'=>'where','field'=>'type_objects_id','value'=>$type));
        
        $order['field'] = 'name';
        $order['direction'] = 'ASC';
        
        $data = $this->_modelInstance->loadList($order,null,$cond);
        
        $res = array(
            "success" => true,
            "results" => $data['results'],
            "rows" => $data['items']
        );
        $this->layout->printLike(json_encode($res), "json");
    }
    
    /*
     * permite validar si el modulo a cargar pertenece a algun grupo del usuario logueado
     */
    private function belongsToGroup($idModule){
        $groups = $this->session->userdata('groups');
        foreach ($groups as $group){
            if($this->auth_model->groupHasModule($group->groups_id,$idModule)){
                return true;
            }
        }
        return false;
    }

    public function index($id)
    {
//        if(!$this->belongsToGroup($id)){
//            header("HTTP/1.1 403 Forbidden . No tiene asignado el recurso solicitado");
//            return;
//        }
        
//        $modulo = $this->_modelInstance->getByid($id);
//        if(is_array($modulo))   $modulo = $modulo[0];
//                
//        $categories = $this->objects_model->getCategories($id);
//        
//        $data['context'] = "module";
//        $banner["module_name"] = $modulo->name;
//        $banner["module_description"] = $modulo->description;
//        $data["module_banner"] = $this->load->view("/include/sections/module_banner/module_title",$banner,true);
//        $data["page_title"] = $modulo->name;
        $data['breadcumb'] = "Tema 1 de ejemplo ";
        $this->layout->view("/object/index",$data);
        
    }
    
    public function modify($id){
        $object = $this->objects_model->getByid($id);
        if(is_array($object))   $object = $object[0];
        
        $data['breadcumb'] = "Modificar objetos";
        $data['object'] = $object;
        $this->layout->view("/object/modify",$data);
    }
    
    public function getClients(){
        
    }
}

